package com.attedu.jdbc;

import com.attedu.jdbc.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/4 - 9:42
 * 查询所有用户的用户名，密码，年龄
 */
public class Demo04JDBC {
    public static void main(String[] args) {
        //自动关闭特性

        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT username, nickname, age FROM user";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            /*
                resultSet.next();
                1.判断是否存在下一个元素
                2.游标cursor初始位置在第一个元素之前
             */
            while (resultSet.next()){
                System.out.print(resultSet.getString("username") + " ");
                System.out.print(resultSet.getString("nickname") + " ");
                System.out.print(resultSet.getInt("age"));
                System.out.println();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
